package states
{
	import core.Assets;
	import core.Game;
	
	import interfaces.IState;
	
	import starling.display.Button;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.text.TextField;
	
	public class GameOver extends Sprite implements IState
	{
		private var _game:Game;
		private var _gameOverText:TextField;
		private var _retryButton:Button;
		
		public function GameOver(game:Game)
		{
			super();
			
			trace("GameOver initialized.");
			this._game = game;
			this.addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, init);
			
			_gameOverText = new TextField(650, 100, "GAME OVER", "Komika Display Kaps Wide", 72, 0xffffff);
			_gameOverText.autoScale = true;
			//_gameOverText.autoSize = TextFieldAutoSize.BOTH_DIRECTIONS;
			_gameOverText.alignPivot("center", "center");
			_gameOverText.x = stage.stageWidth >> 1;
			_gameOverText.y = stage.stageHeight >> 1;
			this.addChild(_gameOverText);
			//_gameOverText.border = true;
			
			// TODO: resize the image in photoshop
			_retryButton = new Button(Assets.atlasTexture.getTexture("retry-button"));
			_retryButton.alignPivot("center", "center");
			_retryButton.x = _gameOverText.x;
			_retryButton.y = _gameOverText.y + (_gameOverText.height >> 1) + _retryButton.height;
			this.addChild(_retryButton);
			_retryButton.addEventListener(Event.TRIGGERED, onRetryClicked);
		}
		
		private function onRetryClicked(e:Event):void
		{
			this.removeEventListener(Event.TRIGGERED, onRetryClicked);
			_game.switchState(Game.PLAY_STATE);
		}
		
		public function update(deltaSmall:Number):void
		{
			
		}
		
		public function destroy():void
		{
			trace("Destroy GameOver");
			this.removeChild(_gameOverText);
			_gameOverText.dispose();
			
			this.removeChild(_retryButton);
			_retryButton.dispose();
			_game = null;
		}
	}
}



































